﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>阻止浏览器默认行为</title>
    <script type="text/javascript">
        window.onload = function () {
            if (window.attachEvent) {//IE 8及以前
                document.getElementById("submit").attachEvent("onclick", preventDefault);
            }
            else {//w3c 
                document.getElementById("submit").addEventListener("click", preventDefault, false);
            }
        };
        function preventDefault(e) {
            if (window.event) {
                // IE
                window.event.returnValue = false;
            }
            else {
                // w3c ff
                e.preventDefault();
            }
        }
    </script>
</head>
<body>
    <form onsubmit="alert('提交成功!');">
    <input id="userName" type="text" value="用户名" />
    <input type="submit" id="submit" value="submit" />
    </form>
</body>
</html>
